<?php

namespace app\admin\controller;

use app\models\{BusinessCommu, BusinessType, AgentCommu, Business, PlatClass, PlatSons, AgentArea, Goods, GoodsSpec, RecpeopleLog, Region, Compent, User as Users, Order, OrderGoods, Recpeople, RecpeopleArea, RecpeopleProject, BussinessLog, Promo, Pkey, Coupon, Agent};
use think\Db;
use think\Validate;

class Shop extends Base
{

    public function businesscommu()
    {
        $model = new BusinessCommu();
        $data = $this->request->get();
        $results = $model->order('id desc');
        if (!empty($data['businessid'])) {
            $results = $results->where('businessid', $data['businessid']);
        }
        $results = $results->paginate(20, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $agentcommu = (new AgentCommu())->where('id', $result['commuid'])->find();
            $result['xiaoqu_name'] = $agentcommu['name'] ?? '';
            $agent = (new Agent())->where('id', ($agentcommu['agentid'] ?? 0))->find();
            $result['agent_name'] = $agent['nickname'] ?? '';
        }
        return view(null, [
            'list' => $results
        ]);
    }


    public function businesscommu_del()
    {
        $id = trim(input('id'), ',');
        $model = new BusinessCommu();
        $res = $model->where(['id' => $id])->delete();
        if ($res) {
            return json(['code' => 0, 'status' => 0, 'data' => [], 'msg' => '操作成功']);
        }
        return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
    }


    public function businesscommu_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'businessid' => 'require',
                'commuid' => 'require',
            ], [
                'businessid.require' => '商家ID不能为空',
                'commuid.require' => '小区不能不正确',
            ]);
            if (!$validate->check($data)) {
                return json(['code' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            $model = new BusinessCommu();
            $haveshop = $model->where(['businessid' => $data['businessid'], 'commuid' => $data['commuid']])->find();
            if (!empty($haveshop) && $haveshop['id'] != $data['id']) {
                return json(['code' => 1, 'data' => [], 'msg' => '商户已绑定该小区,请开启或删除重新添加']);
            }
            if (!empty($data['id'])) {
                $res = $model->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = $model->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
            }
            return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
        } else {
            $model = new BusinessCommu();
            $id = $this->request->get('id/d', 0);
            $row = $model->where('id', $id)->find();
            if (empty($row)) {
                $row['businessid'] = $this->request->get('businessid/d', 0);
            }
            $agentcommu = (new AgentCommu())->select();
            foreach ($agentcommu as $commu) {
                $commu['agent_name'] = (new Agent())->where('id', $commu['agentid'])->value('nickname');
            }
            return view('', [
                'row' => $row,
                'xiaoqu' => $agentcommu
            ]);
        }
    }

    public function compent_add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'specid' => 'require',
                'star' => 'require|in:1,2,3,4,5',
                'text' => 'require',
                'nickName' => 'require',
                'avatarUrl' => 'require',
            ], [
                'specid.require' => '规格ID不能为空',
                'star.require' => '星级不能不能为空',
                'star.in' => '星级类型不正确',
                'nickName.require' => '昵称不能为空',
                'avatarUrl.require' => '头像不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['code' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            if (!empty($data['images'])) {
                $data['images'] = json_encode($data['images'], true);
            } else {
                $data['images'] = '';
            }
            $goodmodel = new Goods();
            $specmodel = new GoodsSpec();
            //查找规格信息
            $spec = $specmodel->where(['id' => $data['specid'], 'isdel' => 1])->find();
            if (empty($spec)) {
                return json(['code' => 1, 'data' => [], 'msg' => '规格信息不存在']);
            }
            $data['goodsid'] = $spec['goodid'];
            $data['businessid'] = $spec['businessid'];
            Db::startTrans();
            try {
                (new Compent())->allowfield(true)->save($data);
                if ($data['star'] > 3) {
                    $goodmodel->where('id', $spec['goodid'])->Inc(['goodspj' => 1, 'sale' => 1])->update();
                }
                Db::commit();
            } catch (\Exception $e) {
                dump($e->getMessage());
                Db::rollback();
                return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
            }
            return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
        } else {
            $specmodel = new GoodsSpec();
            $goodsid = $this->request->get('goodsid/d', 0);
            $results = (new GoodsSpec())->where(['isdel' => 1, 'goodid' => $goodsid])->select();
            return view(null, [
                'row' => $results
            ]);
        }
    }

    public function users()
    {
        $businessid = $this->request->get('business/d', 0);
        $results = (new Users())->alias('u')
            ->where(['o.status' => 4, 'o.businessid' => $businessid])
            ->distinct(true)
            ->join('order o', 'o.user_id= u.id')->field('u.*');
        $results = $results->paginate(20, false, ['query' => $this->request->param()]);
        return view(null, [
            'list' => $results
        ]);
    }

    public function businesslog()
    {
        $model = new BussinessLog();
        $data = $this->request->get();
        $results = $model->order('id desc');
        if (!empty($data['id'])) {
            $results = $results->where('user_id', $data['id']);
        }
        $results = $results->paginate(20, false, ['query' => $this->request->param()]);
        return view(null, [
            'list' => $results
        ]);
    }

    public function shoplist()
    {
        $model = new Business();
        $data = $this->request->get();
        $results = $model->order('id desc')->where(['status' => [1, 2, 3]]);
        if (!empty($data['title'])) {
            $results = $results->where('title|account', 'like', '%' . $data['title'] . '%');
        }
        if (!empty($data['xiaoquid'])) {

            $businessid = (new BusinessCommu())->where('commuid', $data['xiaoquid'])->column('businessid') ?? [];
            if (empty($businessid)) {
                $businessid = [0];
            }
            $results = $results->where(['id' => $businessid]);
        }
        if (!empty($data['status'])) {
            $results = $results->where('status', $data['status']);
        } else {
            $results = $results->where(['status' => [1]]);
        }
        $results = $results->paginate(20, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            if (!empty($xiaoqu)) {
                $result['agent_name'] = (new Agent())->where('id', $xiaoqu['agentid'])->value('nickname');
            } else {
                $result['agent_name'] = '无';
            }
            $result['income'] = (new BussinessLog())->where(['user_id' => $result['id'], 'type' => 1])->sum('money') ?? 0;
            $result['goods'] = (new Goods())->where(['businessid' => $result['id'], 'isSale' => 1, 'isdel' => 1])->count();
            $result['sonstype_name'] = '';
            if ($result['sonstype'] > 0) {
                $result['sonstype_name'] = (new BusinessType())->where('id', $result['sonstype'])->value('name');
            }
        }
        $agentcommu = (new AgentCommu())->select();
        foreach ($agentcommu as $commu) {
            $commu['agent_name'] = (new Agent())->where('id', $commu['agentid'])->value('nickname');
        }
        return view(null, [
            'list' => $results,
            'xiaoqu' => $agentcommu
        ]);
    }

    public function shoplist_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'type' => 'require|in:1,2',
                'title' => 'require',
                'remark' => 'require',
                'account' => 'require',
                'status' => 'require',
                'images' => 'require',
                'fwtime' => 'require',
            ], [
                'type.require' => '类型不能为空',
                'type.in' => '商家类型不正确',
                'title.require' => '商家名称不能为空',
                'remark.require' => '商家备注不能为空',
                'account.require' => '商家备注不能为空',
                'status.require' => '商家状态不能为空',
                'status.require' => '店铺图片不能为空',
                'fwtime.require' => '服务时间不能为空'
            ]);
            if (!$validate->check($data)) {
                return json(['code' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            $data['images'] = $data['images'] ?? [];
            $images = [];
            foreach ($data['images'] as $image) {
                if (!empty($image)) {
                    $images[]['url'] = $image;
                }
            }
            $data['images'] = json_encode($images, true);
            $data['yyzz'] = $data['yyzz'] ?? [];
            $yimages = [];
            foreach ($data['yyzz'] as $image) {
                if (!empty($image)) {
                    $yimages[]['url'] = $image;
                }
            }
            $data['yyzz'] = json_encode($yimages, true);
            if ($data['type'] == 1) {
                $data['sonstype'] = 0;
            } else {
                if (empty($data['sonstype'])) {
                    return json(['code' => 1, 'data' => [], 'msg' => '请选择服务类型']);
                }
            }
            $fwtime = explode('-', $data['fwtime']);
            if (count($fwtime) != 2) {
                return json(['code' => 1, 'data' => [], 'msg' => '时间格式错误']);
            }
            if (strtotime($fwtime[0]) > strtotime($fwtime[1])) {
                return json(['code' => 1, 'data' => [], 'msg' => '时间范围错误']);
            }
            if (abs(strtotime($fwtime[0]) - strtotime($fwtime[1])) < 3600) {
                return json(['code' => 1, 'data' => [], 'msg' => '时间间隔不能低于一个小时']);
            }
            if (empty($data['id']) && empty($data['password'])) {
                return json(['code' => 1, 'data' => [], 'msg' => '商家密码必须填写']);
            }
            if (!empty($data['id']) && empty($data['password'])) {
                unset($data['password']);
            }
            if (!empty($data['password'])) {
                $data['password'] = md5(md5($data['password']));
            }
            $model = new Business();
            //查找店铺名称、账号是否已经被使用
            $haveshop = $model->where('account', $data['account'])->find();
            if (!empty($data['id'])) {
                if (!empty($haveshop) && $haveshop['id'] != $data['id']) {
                    return json(['code' => 1, 'data' => [], 'msg' => '店铺账号已被使用']);
                }
                $res = $model->allowfield(true)->save($data, ['id' => $data['id']]);
                if ($res) {
                    return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
                }
                return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
            } else {
                if (!empty($haveshop)) {
                    return json(['code' => 1, 'data' => [], 'msg' => '店铺账号已被使用']);
                }
                Db::startTrans();
                try {
                    $model->strict(false)->insert($data);
                    Db::commit();
                } catch (\Exception $e) {
                    dump($e->getMessage());
                    Db::rollback();
                    return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
                }
                return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
            }
        } else {
            $model = new Business();
            $id = $this->request->get('id/d', 0);
            $row = $model->where('id', $id)->find();
            if ($row) {
                $row['images'] = json_decode($row['images'], true) ?? [];
                $row['yyzz'] = json_decode($row['yyzz'], true) ?? [];
            }
            $agentcommu = (new AgentCommu())->select();
            foreach ($agentcommu as $commu) {
                $commu['agent_name'] = (new Agent())->where('id', $commu['agentid'])->value('nickname');
            }
            $businesstype = (new BusinessType())->select();
            return view('', [
                'row' => $row,
                'xiaoqu' => $agentcommu,
                'businesstype' => $businesstype
            ]);
        }
    }

    public function compent()
    {
        $model = new Compent();
        $data = $this->request->get();
        $results = $model->order('id desc');
        if (!empty($data['id'])) {
            $results = $results->where('goodsid', $data['id']);
        }
        $results = $results->paginate(20, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['images'] = json_decode($result['images'], true);
            if (!empty($result['nickName'])) {
                $user['nickName'] = $result['nickName'];
                $user['avatarUrl'] = $result['avatarUrl'];
                $result['user'] = $user;
            } else {
                $result['user'] = (new Users())->field('nickName,avatarUrl')->where('id', $result['user_id'])->find();
            }
            $result['spec_name'] = (new GoodsSpec())->where('id', $result['specid'])->value('name');
        }
        return view(null, [
            'list' => $results
        ]);
    }

    public function goodslist()
    {
        $model = new Goods();
        $shopmodel = new Business();
        $data = $this->request->get();
        $results = $model->where('isdel', 1)->order('sorts desc');
        if (!empty($data['title'])) {
            $results = $results->where('title', 'like', '%' . $data['title'] . '%');
        }
        if (!empty($data['platclassid'])) {
            $ptsons = (new PlatSons())->where('isshow', 1)->where('platclassid', $data['platclassid'])->order(['sort' => 'desc', 'id' => 'desc'])->select();
            $results = $results->where('classid', $data['platclassid']);
        }
        if (!empty($data['type'])) {
            $results = $results->where('type', $data['type']);
        }
        if (!empty($data['sonsid'])) {
            $results = $results->where('sonsid', $data['sonsid']);
        }
        if (!empty($data['businessid'])) {
            $results = $results->where('businessid', $data['businessid']);
        }
        $results = $results->paginate(20, false, ['query' => $this->request->param()]);
        $shop = (new Business())->field('id,title,account')->where('status', 1)->select();
        $platclass = (new PlatClass())->where('isshow', 1)->where('type', 1)->order(['sort' => 'desc', 'id' => 'desc'])->select();
        foreach ($results as $result) {
            $business = $shopmodel->where('id', $result['businessid'])->find();
            $result['images'] = json_decode($result['images'], true) ?? [];
            $result['shopname'] = $business['title'] ?? '';
            $result['btype'] = $business['type'] ?? 0;
            $result['class_name'] = (new PlatClass())->where('id', $result['classid'])->value('name');
            $result['sons_name'] = (new PlatSons())->where('id', $result['sonsid'])->value('name');
            $result['goodspec'] = (new GoodsSpec())->where(['isdel' => 1, 'goodid' => $result['id']])->order('is_default desc')->find();
        }
        return view(null, [
            'list' => $results,
            'shop' => $shop,
            'platclass' => $platclass
        ]);
    }

    public function goodslist_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'title' => 'require',
                'businessid' => 'require',
                'images' => 'require',
                'classid' => 'require',
                'sonsid' => 'require',
                'spec' => 'require'
            ], [
                'title.require' => '产品名称不能为空',
                'businessid.require' => '商家ID不能为空',
                'images.require' => '图片不能为空',
                'classid.require' => '分类不能为空',
                'sonsid.require' => '子分类不能为空',
                'spec.require' => '规格必须填写'
            ]);
            if (!$validate->check($data)) {
                return json(['code' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            if (!empty($data['images'])) {
                $data['images'] = json_encode($data['images'], true);
            }
            if (!empty($data['parpam'])) {
                $data['parpam'] = json_encode($data['parpam'], true);
            }
            if (empty($data['spec']) || count($data['spec']) <= 0) {
                return json(['code' => 1, 'data' => [], 'msg' => '规格信息不能少于一个']);
            }
            if (isset($data['file'])) {
                unset($data['file']);
            }
            $shopmodel = new Business();
            $goodmodel = new Goods();
            $platsonsmodel = new PlatSons();
            $specmodel = new GoodsSpec();
            //查找商家是否存
            $shoplist = $shopmodel->where('id', $data['businessid'])->find();
            if (!$shoplist) {
                return json(['code' => 1, 'data' => [], 'msg' => '商家不存在']);
            }
            //查找分类是否存在
            $platsons = $platsonsmodel->where(['id' => $data['sonsid']])->find();
            if (empty($platsons)) {
                return json(['code' => 1, 'data' => [], 'msg' => '商家分类不存在']);
            }
            $data['classid'] = $platsons['platclassid'];
            $insert = $data;
            unset($insert['spec']);
            if (empty($data['id'])) {
                Db::startTrans();
                try {
                    $data['id'] = $goodmodel->insertGetid($insert);
                    foreach ($data['spec'] as $spec) {
                        if (!empty($spec['id'])) {
                            $specmodel->where(['goodid' => $data['id'], 'id' => $spec['id']])->update($spec);
                        } else {
                            unset($spec['id']);
                            $spec['businessid'] = $shoplist['id'];
                            $spec['goodid'] =  $data['id'];
                            $specmodel->insert($spec);
                        }
                    }
                    Db::commit();
                } catch (\Exception $e) {
                    dump($e->getMessage());
                    Db::rollback();
                    return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
                }
            } else {
                Db::startTrans();
                try {
                    $goodmodel->where('id', $data['id'])->update($insert);
                    foreach ($data['spec'] as $spec) {
                        if (!empty($spec['id'])) {
                            $specmodel->where(['goodid' => $data['id'], 'id' => $spec['id']])->update($spec);
                        } else {
                            unset($spec['id']);
                            $spec['businessid'] = $shoplist['id'];
                            $spec['goodid'] =  $data['id'];
                            $specmodel->insert($spec);
                        }
                    }
                    Db::commit();
                } catch (\Exception $e) {
                    dump($e->getMessage());
                    Db::rollback();
                    return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
                }
            }
            return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
        } else {
            $model = new Goods();
            $specmodel = new GoodsSpec();
            $shopmodel = new Business();
            $id = $this->request->get('id/d', 0);
            $results = $model->where('id', $id)->find();
            $platsons = [];
            $shoplist = $shopmodel->field('id,title,account')->where('type', 1)->select();
            if ($results) {
                $results['images'] = json_decode($results['images'], true) ?? [];
                $results['parpam'] = json_decode($results['parpam'], true) ?? [];
                $results['speclist'] = $specmodel->order('is_default desc')->where(['businessid' => $results['businessid'], 'isdel' => 1, 'goodid' => $results['id']])->select();
                $platsons = (new PlatSons())->where('platclassid', $results['classid'])->where('isshow', 1)->select();
            } else {
                $results['businessid'] = $this->request->get('businessid/d', 0);
            }
            $platclass = (new PlatClass())->where('isshow', 1)->where(['type' => [1, 3]])->select();
            return view(null, [
                'row' => $results,
                'shoplist' => $shoplist,
                'platclass' => $platclass,
                'platsons' => $platsons
            ]);
        }
    }

    public function goods_sort()
    {
        $data = $this->request->post();
        $validate = new Validate([
            'id' => 'require',
            'sorts' => 'require|egt:0',
        ], [
            'id.require' => 'ID不能为空',
            'sorts.require' => '数值不能为空',
            'sorts.gt' => '数值必须大于等于0',
        ]);
        if (!$validate->check($data)) {
            return json(['code' => 1, 'data' => [], 'msg' => $validate->getError()]);
        }
        $res = (new Goods())->where('id', $data['id'])->update(['sorts' => $data['sorts']]);
        if ($res) {
            return json(['status' => 0, 'data' => [], 'msg' => '操作成功']);
        }
        return json(['status' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
    }

    public function goodslist_edit2()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'title' => 'require',
                'businessid' => 'require',
                'classid' => 'require',
                'images' => 'require',
                'spec' => 'require'
            ], [
                'title.require' => '服务名称不能为空',
                'businessid.require' => '商家ID不能为空',
                'classid.require' => '分类不能为空',
                'images.require' => '图片不能为空',
                'spec.require' => '规格必须填写'
            ]);
            if (!$validate->check($data)) {
                return json(['code' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            if (!empty($data['images'])) {
                $data['images'] = json_encode($data['images'], true);
            }
            if (!empty($data['parpam'])) {
                $data['parpam'] = json_encode($data['parpam'], true);
            }
            if (empty($data['spec']) || count($data['spec']) <= 0) {
                return json(['code' => 1, 'data' => [], 'msg' => '规格信息不能少于一个']);
            }
            if (isset($data['file'])) {
                unset($data['file']);
            }
            $shopmodel = new Business();
            $goodmodel = new Goods();
            $specmodel = new GoodsSpec();
            //查找商家是否存
            $shoplist = $shopmodel->where('id', $data['businessid'])->find();
            if (!$shoplist) {
                return json(['code' => 1, 'data' => [], 'msg' => '商家不存在']);
            }
            $data['sonsid'] = 0;
            $data['classid'] = $platsons['platclassid'] ?? $data['classid'];
            $insert = $data;
            unset($insert['spec']);
            if (empty($data['id'])) {
                Db::startTrans();
                try {
                    $data['id'] = $goodmodel->insertGetid($insert);
                    foreach ($data['spec'] as $spec) {
                        if (!empty($spec['id'])) {
                            $specmodel->where(['goodid' => $data['id'], 'id' => $spec['id']])->update($spec);
                        } else {
                            unset($spec['id']);
                            $spec['businessid'] = $shoplist['id'];
                            $spec['goodid'] =  $data['id'];
                            $specmodel->insert($spec);
                        }
                    }
                    Db::commit();
                } catch (\Exception $e) {
                    dump($e->getMessage());
                    Db::rollback();
                    return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
                }
            } else {
                Db::startTrans();
                try {
                    $goodmodel->where('id', $data['id'])->update($insert);
                    foreach ($data['spec'] as $spec) {
                        if (!empty($spec['id'])) {
                            $specmodel->where(['goodid' => $data['id'], 'id' => $spec['id']])->update($spec);
                        } else {
                            unset($spec['id']);
                            $spec['businessid'] = $shoplist['id'];
                            $spec['goodid'] =  $data['id'];
                            $specmodel->insert($spec);
                        }
                    }
                    Db::commit();
                } catch (\Exception $e) {
                    dump($e->getMessage());
                    Db::rollback();
                    return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
                }
            }
            return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
        } else {
            $model = new Goods();
            $specmodel = new GoodsSpec();
            $shopmodel = new Business();
            $cate = [];
            $id = $this->request->get('id/d', 0);
            $results = $model->where('id', $id)->find();
            $platsons = [];
            $shoplist = $shopmodel->field('id,title,account')->where('type', 2)->select();
            if ($results) {
                $results['images'] = json_decode($results['images'], true) ?? [];
                $results['parpam'] = json_decode($results['parpam'], true) ?? [];
                $results['speclist'] = $specmodel->order('is_default desc')->where(['businessid' => $results['businessid'], 'isdel' => 1, 'goodid' => $results['id']])->select();
                $platsons = (new PlatSons())->where('platclassid', $results['classid'])->where('isshow', 1)->select();
            }
            $platclass = (new PlatClass())->where('isshow', 1)->where('type', 2)->select();
            return view(null, [
                'row' => $results,
                'shoplist' => $shoplist,
                'platclass' => $platclass,
                'platsons' => $platsons
            ]);
        }
    }

    public function goodslist_del()
    {
        $id = trim(input('id'), ',');
        $model = new Goods();
        $res = $model->where(['id' => $id])->update(['isdel' => 2]);
        if ($res) {
            return json(['code' => 0, 'status' => 0, 'data' => [], 'msg' => '操作成功']);
        }
        return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
    }

    public function goodsspec_del()
    {
        $id = trim(input('id'), ',');
        $model = new GoodsSpec();
        $goodsspec = $model->where(['id' => $id, 'isdel' => 1])->find();
        if (empty($goodsspec)) {
            return json(['code' => 1, 'data' => [], 'msg' => '库存规格不存在']);
        }
        //查询规格商品是否超过1个
        $havespec = $model->where(['goodid' => $goodsspec['goodid'], 'isdel' => 1])->count();
        if ($havespec <= 1) {
            return json(['code' => 1, 'data' => [], 'msg' => '库存规格必须保留一个']);
        }
        $res = $model->where(['id' => $id])->update(['isdel' => 2]);
        if ($res) {
            return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
        }
        return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
    }

    public function goods_field()
    {
        if ($this->request->isPost()) {
            $id = array_filter(input('id'));
            if (empty($id)) {
                return json(['status' => 1, 'msg' => '请选择操作选项']);
            }
            $isshow = $this->request->post('status/d', 0);
            $field = $this->request->post('field', '');
            if (empty($isshow) || empty($field)) {
                return json(['status' => 1, 'msg' => '参数值错误']);
            }
            $res = (new Goods())->where(['id' => $id])->update([$field => $isshow]);
            if ($res) {
                return json(['status' => 0, 'msg' => '操作成功']);
            }
            return json(['status' => 1, 'msg' => '操作失败']);
        }
    }
}
